#include <windows.h>
#include <assert.h>
#include <stdio.h>
#include "tac.h"

TAC::TAC()
: m_fpTAStart(NULL)
, m_fpTAPutMsg(NULL)
, m_fpTAPutMsgExt(NULL)
, m_fpTAGetMsg(NULL)
, m_fpTAGetMsgExt(NULL)
, m_fpTAGetLinkStatus(NULL)
, m_fpTAClose(NULL)
, m_hTAC(NULL)
, m_pConfig(NULL)
{
	m_pConfig = new Config;
}

TAC::~TAC()
{
	if(m_hTAC)
	{
		FreeLibrary((HINSTANCE)m_hTAC);
		m_hTAC = NULL;
	}
	
	delete m_pConfig;
	m_pConfig = NULL;
}

int TAC::TAStart()
{
	if(m_fpTAStart)
		return (*m_fpTAStart)();
	else
		return LIB_NULL;
}

int TAC::TAPutMsg(char *pcpDest, char *pcpMsg)
{
	assert(pcpDest && pcpMsg);
	
	if(m_fpTAPutMsg)
		return (*m_fpTAPutMsg)(pcpDest, pcpMsg);
	else
		return LIB_NULL;
}

int TAC::TAPutMsgExt(char *pcpMsg)
{
	assert(pcpMsg);
	
	if(m_fpTAPutMsgExt)
		return (*m_fpTAPutMsgExt)(pcpMsg);
	else
		return LIB_NULL;
}

int TAC::TAGetMsg(char *pcpOrg, char *pcpMsg)
{
	assert(pcpOrg && pcpMsg);
	
	if(m_fpTAGetMsg)
		return (*m_fpTAGetMsg)(pcpOrg, pcpMsg);
	else
		return LIB_NULL;
}

int TAC::TAGetMsgExt(char *pcpMsg)
{
	assert(pcpMsg);
	
	if(m_fpTAGetMsgExt)
		return (*m_fpTAGetMsgExt)(pcpMsg);
	else
		return LIB_NULL;
}

int TAC::TAGetLinkStatus()
{
	if(m_fpTAGetLinkStatus)
		return (*m_fpTAGetLinkStatus)();
	else
		return LIB_NULL;
}

int TAC::TAClose()
{
	if(m_fpTAClose)
		return (*m_fpTAClose)();
	else
		return LIB_NULL;
}

int TAC::Init()
{
	int nRt = 0;
	m_hTAC = LoadLibrary(LIB_TAC);
	
	if(m_hTAC)
	{
#ifdef DEBUG
	printf("init tac ok\n");
#endif
		m_fpTAStart = (fpTAStart)GetProcAddress((HINSTANCE)m_hTAC, "TAStart");
		m_fpTAPutMsg = (fpTAPutMsg)GetProcAddress((HINSTANCE)m_hTAC, "TAPutMsg");
		m_fpTAPutMsgExt = (fpTAPutMsgExt)GetProcAddress((HINSTANCE)m_hTAC, "TAPutMsgExt");
		m_fpTAGetMsg = (fpTAGetMsg)GetProcAddress((HINSTANCE)m_hTAC, "TAGetMsg");
		m_fpTAGetMsgExt = (fpTAGetMsgExt)GetProcAddress((HINSTANCE)m_hTAC, "TAGetMsgExt");
		m_fpTAGetLinkStatus = (fpTAGetLinkStatus)GetProcAddress((HINSTANCE)m_hTAC, "TAGetLinkStatus");
		m_fpTAClose = (fpTAClose)GetProcAddress((HINSTANCE)m_hTAC, "TAClose");
	}
	else
	{
		nRt = -1;
	}
	
	return nRt;
}

int32 TAC::LoadConfig()
{
	int32 nRt = 0;
	
	return nRt;
}

int32 TAC::SetConfig(char8 *pIP, char8 *pPort, char8 *pDest)
{
	int32 nRt = 0;
	
	return nRt;
}